﻿#include <Windows.h>

#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/MatrixTransform>
#include <osgGA/TerrainManipulator>
#include <osgGA/FlightManipulator>
#include <osgUtil/Optimizer>
#include <osg/ClipPlane>
#include <osg/Scissor>
#include "FrameEventHandler.h"
#include "GeometryProduct.h"

class mycallback :public osg::NodeCallback
{
public:
	mycallback(float pos)
	{
		m_pos = pos;
	}
	virtual void operator() (osg::Node* node, osg::NodeVisitor* nv)
	{
		osg::ref_ptr<osg::MatrixTransform> transpos = dynamic_cast<osg::MatrixTransform*>(node);
		if (nv && transpos && nv->getFrameStamp())
		{

			//double time=nv->getFrameStamp()->getReferenceTime();获取当前运行时间
			m_pos++;
			//飞机运行下一个位置，可以在此设置translate三个参数数学值，让它实现在固定路劲上飞行
			transpos->setMatrix(osg::Matrix::translate(0.0f + cosf(0.02 * m_pos), 1.0f, 0.0f + sinf(0.02 * m_pos)) *
				osg::Matrix::rotate(60, 1.0f, 0.0f, 0.0f));
			//Sleep(1000);
		}
		traverse(node, nv);
	}
protected:
	int m_pos;
};

int main()
{
	//主函数
	//创建viewer
	osgViewer::Viewer  viewer;
	//创建group组节点
	osg::ref_ptr<osg::Group> root = new osg::Group();
	//加载模型NODE
	//osg::ref_ptr<osg::Node>firnode1 = osgDB::readNodeFile("E:\\OpenSceneGraph-Data\\glider.osg");

	//osg::ref_ptr<osg::Node>robotnode = osgDB::readNodeFile("E:\\OpenSceneGraph-Data\\robot.osg");
	////创建模型矩阵变换matrix节点
	//osg::ref_ptr<osg::MatrixTransform> transpos = new osg::MatrixTransform();

	//osg::ref_ptr<osg::MatrixTransform> transpos2 = new osg::MatrixTransform();
	//transpos2->setMatrix(osg::Matrix::scale(0.2f, 0.2f, 0.2f) * osg::Matrix::translate(0.0f, 0.0f, -3.0f));
	////设置回调
	////transpos->setUpdateCallback(new mycallback(1));
	////为回调传入模型指针
	//transpos->addChild(firnode1);
	//transpos2->addChild(robotnode);
	////将变换节点加入组节点
	//root->addChild(transpos);
	//root->addChild(transpos2);

	//
	////加载viewer显示播放
	//viewer.setSceneData(root);
	//viewer.addEventHandler(new FrameEventHandler());
	//FirViewer.setCameraManipulator(new osgGA::TerrainManipulator());
	//FirViewer.setCameraManipulator(new osgGA::FlightManipulator());
	root->addChild(GeometryProduct::createCar());
	//root->addChild(GeometryProduct::createShape());

	//创建一个裁剪平面
	//osg::ref_ptr<osg::ClipPlane> cp1 = new osg::ClipPlane();
	////设置裁剪平面
	//cp1->setClipPlane(0, 1, 1, 1);
	////指定平面索引
	//cp1->setClipPlaneNum(0);
	//osg::ref_ptr<osg::ClipPlane> cp2 = new osg::ClipPlane();
	//cp2->setClipPlane(1, 0, 0, 1);
	//cp2->setClipPlaneNum(1);
	//root->getOrCreateStateSet()->setAttributeAndModes(cp1.get(), osg::StateAttribute::ON);
	//root->getOrCreateStateSet()->setAttributeAndModes(cp2.get(), osg::StateAttribute::ON);
	//osg::ref_ptr<osg::Scissor> cc = new osg::Scissor();
	//cc->setScissor(150, 150, 800, 600);
	//root->getOrCreateStateSet()->setAttributeAndModes(cc.get(), osg::StateAttribute::ON);


	osgUtil::Optimizer optimizer;
	optimizer.optimize(root.get());
	
	viewer.setSceneData(root.get());
	viewer.realize();
	viewer.run();


	return 0;
}
